package com.yjhb.android.sbz.fragment;

import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.activity.ManageTaskActivity;
import com.yjhb.android.sbz.activity.MsgActivity;
import com.yjhb.android.sbz.activity.MyTaskActivity;
import com.yjhb.android.sbz.activity.TipOffActivity;
import com.yjhb.android.sbz.manager.ResManager;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.BaseFragment;
import com.jojo.android.zxlib.base.mvp.BasePresenter;
import com.jojo.android.zxlib.tool.ZxActivityTool;
import com.qmuiteam.qmui.widget.popup.QMUIListPopup;
import com.qmuiteam.qmui.widget.popup.QMUIPopup;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tencent.qcloud.tim.uikit.Observer;
import com.tencent.qcloud.tim.uikit.UnReadManager;
import com.tencent.qcloud.tim.uikit.base.IUIKitCallBack;
import com.tencent.qcloud.tim.uikit.component.action.PopDialogAdapter;
import com.tencent.qcloud.tim.uikit.component.action.PopMenuAction;
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationListAdapter;
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationListLayout;
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationManagerKit;
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationProvider;
import com.tencent.qcloud.tim.uikit.modules.conversation.base.ConversationInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/23 9:52
 */
public class MsgNoticeFragment extends BaseFragment implements IUIKitCallBack, ConversationListLayout.OnItemClickListener,
        OnRefreshListener, ConversationListLayout.OnItemLongClickListener, Observer<Integer> {
    private final String TAG = "IMFragment";

    @BindView(R.id.conversationListLayout)
    ConversationListLayout conversationListLayout;
    @BindView(R.id.notMore_tv)
    TextView notMoreTv;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.reportUnread_tv)
    TextView reportUnread_tv;
    @BindView(R.id.msgTask_tv)
    TextView msgTaskTv;
    @BindView(R.id.msgCheck_tv)
    TextView msgCheckTv;

    ConversationListAdapter adapter;

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_msg_notice;
    }

    @Override
    protected void initUI() {
        adapter = new ConversationListAdapter();
        conversationListLayout.setAdapter(adapter);

    }

    @Override
    protected void initEvent() {
        adapter.setOnItemClickListener(this);
        adapter.setOnItemLongClickListener(this);
        smartRefreshLayout.setOnRefreshListener(this);
        UnReadManager.getInstance().register(this);
    }


    @Override
    protected void initData() {
        ConversationManagerKit.getInstance().loadConversation(this);
    }

    @Override
    public void update(int type, Integer arg) {

        int reportUnread = UnReadManager.getInstance().getUnreadReport();
        int taskUnread = UnReadManager.getInstance().getUnreadTask();
        int checkUnread = UnReadManager.getInstance().getUncheck();
//        if (type == UnReadManager.TYPE_REPORT) {
        if (reportUnread > 0) {
            if (reportUnread > 99) reportUnread = 99;
            reportUnread_tv.setVisibility(View.VISIBLE);
            reportUnread_tv.setText("" + reportUnread);
        } else {
            reportUnread_tv.setVisibility(View.GONE);
        }
        if (checkUnread > 0) {
            if (checkUnread > 99) checkUnread = 99;
            msgCheckTv.setVisibility(View.VISIBLE);
            msgCheckTv.setText("" + checkUnread);
        } else {
            msgCheckTv.setVisibility(View.GONE);
        }
        if (taskUnread > 0) {
            if (taskUnread > 99) taskUnread = 99;
            msgTaskTv.setVisibility(View.VISIBLE);
            msgTaskTv.setText("" + taskUnread);
        } else {
            msgTaskTv.setVisibility(View.GONE);
        }
//        }
    }

    @Override
    public void onDestroy() {
        UnReadManager.getInstance().unregister(this);
        super.onDestroy();
    }

    @OnClick(R.id.msgView)
    void msgView() {
        ZxActivityTool.skipActivity(getContext(), MsgActivity.class);
    }

    @OnClick(R.id.msgTask)
    void msgTask() {
        ZxActivityTool.skipActivity(getContext(), MyTaskActivity.class);
        UnReadManager.getInstance().setUnreadTask(0);
    }

    @OnClick(R.id.msgCheck)
    void msgCheck() {
        ZxActivityTool.skipActivity(getContext(), ManageTaskActivity.class);
        UnReadManager.getInstance().setUncheck(0);
    }

    @OnClick(R.id.msgReport)
    void msgReport() {
        ZxActivityTool.skipActivity(getContext(), TipOffActivity.class);
        UnReadManager.getInstance().setUnreadReport(0);
    }


    @Override
    public void onSuccess(Object data) {
        if (adapter != null)
            adapter.setDataProvider((ConversationProvider) data);
        if (smartRefreshLayout != null)
            smartRefreshLayout.finishRefresh();
    }

    @Override
    public void onError(String module, int errCode, String errMsg) {

    }


    @Override
    public void onItemClick(View view, int position, ConversationInfo messageInfo) {
        Tool.startChatActivity(getContext(),messageInfo.getId(),messageInfo.getTitle(),0);
    }


    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        ConversationManagerKit.getInstance().loadConversation(this);
    }

    @Override
    public void OnItemLongClick(View view, int position, ConversationInfo messageInfo) {
        PopDialogAdapter adapter = null;
        QMUIListPopup popup = new QMUIListPopup(getContext(), QMUIPopup.DIRECTION_TOP, adapter = new PopDialogAdapter());
        popup.create(100 * ResManager.dp(), -2, new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position2, long id) {
                if (position2 == 0) {
                    ConversationManagerKit.getInstance().deleteConversation(position, messageInfo);
                    popup.dismiss();
                }
            }
        });
        List<PopMenuAction> actions = new ArrayList<>();
        PopMenuAction action = new PopMenuAction();
        action.setActionName("刪除");
        actions.add(action);
        adapter.setDataSource(actions);
        popup.show(view);
    }


}
